package com.informatics.polymer.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.informatics.polymer.client.PolymerTemplates.HomopolymerPanel;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.widgets.HTMLFlow;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.grid.events.CellClickEvent;
import com.smartgwt.client.widgets.grid.events.CellClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class PolyGWTCSDS2 implements EntryPoint {
	/**
	 * Array of TreeRecord. The data to populate the polymer templates for
	 * building polymers.
	 */
	private TreeRecord[] polymerTemplates = new TreeRecord[6];
	private HomopolymerPanel homopolymerPanel;

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
/*		//Tester button
		Button b = new Button("testRestlet");
		b.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				//Need to add restlet code here to process the polymer.
				 final Client client = new Client(Protocol.HTTP);
				 client.get("http://localhost:8080/homopolymer", new Callback() {
						@Override
						public void onEvent(Request request,
								org.restlet.gwt.data.Response response) {
					     	com.google.gwt.user.client.Window.alert(response.getEntity().getText());		
						}
	                });
			}});
	*/
				/////////////////////////////////
		final HLayout layout = new HLayout();
		final SectionStack sectionStack = new SectionStack();
		sectionStack.setVisibilityMode(VisibilityMode.MULTIPLE);
		sectionStack.setWidth(200);
		sectionStack.setHeight(500);
		sectionStack.setBorder("2px solid #458B00");
		SectionStackSection section1 = new SectionStackSection("PolyGWT");
		section1.setExpanded(true);
		HTMLFlow welcome = new HTMLFlow();
		welcome.setOverflow(Overflow.AUTO);
		welcome.setPadding(10);
		String welcomeString = "Welcome to PolyGWT! Below you will find information on how to "
				+ "build polymers, search a database for repeat units based on substructure, search for "
				+ "repeat units based on property values and browse the existing repository of repeat units and end groups.";
		welcome.setContents(welcomeString);
		section1.addItem(welcome);
		sectionStack.addSection(section1);
		sectionStack.setStyleName("sectionHeaderclosed");

		SectionStackSection section2 = new SectionStackSection(
				"Build A Polymer");
		section2.setExpanded(false);
		section2.setCanCollapse(true);
		Tree polymerTree = new Tree();
		setTreeNodes();
		polymerTree.setData(polymerTemplates);
		TreeGrid polymerTreeGrid = new TreeGrid();
		polymerTreeGrid.setWidth(200);
		polymerTreeGrid.setHeight(100);
		// TreeGridField tgf = new TreeGridField("rootlVal");
		// TreeGridField ext = new TreeGridField("ExtraId");
		polymerTreeGrid.setFields(new TreeGridField("Polymer"));
		// Add cell clickhandler
		polymerTreeGrid.addCellClickHandler(new CellClickHandler() {
			@Override
			public void onCellClick(CellClickEvent event) {
				int selectedRow = event.getRowNum();
				// Window.alert(Integer.toString(selectedRow));
				if (selectedRow == 0) {
					Window.alert("Homopolymer panel being created!");
					homopolymerPanel = new HomopolymerPanel();
					layout.addMember(homopolymerPanel);
					
				} else if (selectedRow == 1) {
					Window.alert("Block copolymer panel being created!");
				} else if (selectedRow == 2) {
					Window.alert("Alternating copolymer panel being created!");
				} else if (selectedRow == 3) {
					Window.alert("Graft copolymer panel being created!");
				} else if (selectedRow == 4) {
					Window.alert("Random copolymer panel being created!");
				} else if (selectedRow == 5) {
					Window.alert("Dendrimer polymer panel being created!");
				}

			}

		});
		polymerTreeGrid.setData(polymerTree);
		polymerTreeGrid.getData().openAll();
		polymerTreeGrid.draw();
		section2.addItem(polymerTreeGrid);
		sectionStack.addSection(section2);
		sectionStack.setStyleName("sectionHeaderclosed");

		SectionStackSection section3 = new SectionStackSection(
				"Substructure Search");
		section3.setExpanded(false);
		section3.setCanCollapse(true);
		section3.addItem(new Img("pieces/48/pawn_green.png", 48, 48));
		sectionStack.addSection(section3);
		sectionStack.setStyleName("sectionHeaderclosed");

		SectionStackSection section4 = new SectionStackSection(
				"Property Search");
		section4.setExpanded(false);
		section4.setCanCollapse(true);
		section4.addItem(new Img("pieces/48/piece_yellow.png", 48, 48));
		sectionStack.addSection(section4);
		sectionStack.setStyleName("sectionHeaderclosed");

		SectionStackSection section5 = new SectionStackSection(
				"Browse Repository");
		section5.setExpanded(false);
		section5.setCanCollapse(true);
		section5.addItem(new Img("pieces/48/piece_yellow.png", 48, 48));
		sectionStack.addSection(section5);
		sectionStack.setStyleName("sectionStack");
		sectionStack.setBackgroundColor("#F0F0F0");
		layout.setMembersMargin(20);
		
//		layout.addChild(b);
		
		layout.addMember(sectionStack);
		VLayout buttons = new VLayout();
		buttons.setMembersMargin(10);
		layout.addMember(buttons);
		layout.draw();
	}

	/**
	 * Method to populate the polymer templates in the accordion layout.
	 */
	public void setTreeNodes() {
		polymerTemplates[0] = new TreeRecord("HomoPolymer", "Polymer", "Ho");
		polymerTemplates[1] = new TreeRecord("BlockCopolymer", "Polymer", "BC");
		polymerTemplates[2] = new TreeRecord("AlternatingCopolymer", "Polymer",
				"AC");
		polymerTemplates[3] = new TreeRecord("GraftCopolymer", "Polymer", "GC");
		polymerTemplates[4] = new TreeRecord("RandomCopolymer", "Polymer", "Ro");
		polymerTemplates[5] = new TreeRecord("Dendrimer", "Polymer", "D");
	}
}
